package com.theaf.core.configuration;

import com.theaf.core.mq.properties.ConsumerProperties;
import com.theaf.core.mq.properties.MqBaseProperties;
import com.theaf.core.mq.properties.ProducerProperties;
import com.theaf.core.mq.service.ConsumerServer;
import com.theaf.core.mq.service.ProducerServer;
import com.theaf.core.utils.SpringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 核心包配置
 *
 * @author luojuan
 * @create 2017-11-28 9:49
 **/
@Configuration
@EnableConfigurationProperties({ProducerProperties.class , ConsumerProperties.class , MqBaseProperties.class})
public class ApiConfiguration {


    @Bean
    @Primary
    @ConditionalOnMissingBean(SpringUtils.class)
    public SpringUtils getSpringUtils(){
         return new SpringUtils();
    }

    @Configuration
    protected static class SmywMqConfiguration {

        private final ProducerProperties producerProperties;

        private final ConsumerProperties consumerProperties;

        private final MqBaseProperties mqBaseProperties;

        public SmywMqConfiguration(final ProducerProperties producerProperties ,
                                   final ConsumerProperties consumerProperties ,
                                   final MqBaseProperties mqBaseProperties){
            this.producerProperties = producerProperties;
            this.consumerProperties = consumerProperties;
            this.mqBaseProperties = mqBaseProperties;
        }

        @Bean
        @ConditionalOnProperty(prefix = "smyw.mq" , value = {"accessKey" , "secretKey" , "producerId"})
        @ConditionalOnMissingBean(ProducerServer.class)
        public ProducerServer producerServer(){
            return ProducerServer.getInstance(producerProperties , mqBaseProperties);
        }

        @Bean
        @ConditionalOnProperty(prefix = "smyw.mq" , value = {"accessKey" , "secretKey" , "consumerId" , "topic" , "tag"})
        @ConditionalOnMissingBean(ConsumerServer.class)
        public ConsumerServer consumerServer(){
            ConsumerServer.start(mqBaseProperties.getTopic() , mqBaseProperties.getTag(), consumerProperties);
            return null;
        }
    }
}
